<?php

namespace app\admin\controller\service\order;

use app\common\controller\Backend;
use think\Db;
/**
 * 订单退款管理
 *
 * @icon fa fa-circle-o
 */
class Refundorder extends Backend
{

    /**
     * Refundorder模型对象
     * @var \app\admin\model\service\order\Refundorder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\service\order\Refundorder;
        $this->view->assign("stateList", $this->model->getStateList());
    }



    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list as &$val)
        {
            $val->user = db('user')->where('id',$val->user_id)->field('avatar,nickname,mobile')->find();
            $order = db('service_order')->where(['id'=>$val->order_id])->find();
            $val->address = db('service_order_address')->where('order_id',$val->order_id)->field('name,sex,mobile,province,city,district,area,address')->find();
            $val->detail = db('service_order_detail')->where('order_id',$val->order_id)->field('name,image,sku_name,num,price,sumprice')->find();
            $val->skill = $order['skill_id']?db('service_skill')->field('name,mobile,image')->where('id',$order['skill_id'])->find():'';
            $val->shop = $order['shop_id']?db('service_shop')->where('id',$order['shop_id'])->field('name,abbr,logo_image,leader_name,leader_mobile')->find():'';
            $val->create_time = date("Y-m-d H:i",$val->createtime);
            $val->order = $order;
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }



    public function refund($ids)
    {
        if($this->request->isPost())
        {

            $params = input('post.','');
            $row = $this->model->where('id',$params['id'])->find();
            $row->state != 0 && $this->error('请勿重复审核');
            $this->model->where(['id'=>$params['id']])->update(['note'=>$params['note'],'state'=>-1]);
            \app\admin\model\service\order\Order::where('id',$row->order_id)->update(['is_service'=>-1]);
            $order = \app\admin\model\service\order\Order::where('id',$row->order_id)->field('id,user_id,shop_id,skill_id')->find();
            \app\api\model\service\OrderLog::create(['user_id'=>$order['user_id'],'order_id'=>$order['id'],'type'=>10,'content'=>'申请退款已拒绝']);
            $userOpenid = \app\api\model\service\UserInfo::getOpenid($order['user_id'],0);
            \app\api\model\service\Order::createRefundTemplateParams(['id'=>$order['id'],'state'=>-1,'note'=>$params['note'],'type'=>0,'templateAttr'=>'user_sales_template','openid'=>$userOpenid]);
            $this->success('订单状态已更新');
        }
        $this->assign(['id'=>$ids]);
        return $this->view->fetch();
    }




}
